package cn.sibu.disalllowtouchintercept;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {

    private static final String[] ITEMS = {
            "Row One", "Row Two", "Row Three", "Row Four", "Row Five", "Row Six", "Row Seven", "Row Eight", "Row Nine", "Row Ten"
    };

    private ViewPager mViewPager;
    private ListView mListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //创建水平轻扫条目的头部视图
        mViewPager = new ViewPager(this);
        //作为ListView的头部,ViewPager必须有固定的高度
        mViewPager.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, getResources().getDimensionPixelSize(R.dimen.header_height)));
        //侦听分页状态变化以禁止父视图触摸
        mViewPager.addOnPageChangeListener(this);
        mViewPager.setAdapter(new HeaderAdapter(this));
        //创建垂直滚动列表
        mListView = new ListView(this);
        //添加页面调度程序作为列表头部
        mListView.addHeaderView(mViewPager);
        //添加列表条目
        mListView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ITEMS));
        setContentView(mListView);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {
        //当ViewPager滚动时,禁止ScrollView触摸拦截
        //从而使它不能接管并尝试垂直滚动
        //必须为每个要重写的手势设置此标志
        boolean isScrolling = (state != ViewPager.SCROLL_STATE_IDLE);
        Log.e("isScrolling",""+isScrolling);
        mListView.requestDisallowInterceptTouchEvent(isScrolling);
    }

    private static class HeaderAdapter extends PagerAdapter {

        private Context mContext;

        public HeaderAdapter(Context context) {
            mContext = context;
        }


        @Override
        public int getCount() {
            return 5;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            //创建新的页面视图
            TextView tv = new TextView(mContext);
            tv.setText(String.format("Page %d", position + 1));
            tv.setBackgroundColor((position % 2 == 0) ? Color.RED : Color.GREEN);

            tv.setGravity(Gravity.CENTER);
            tv.setTextColor(Color.BLACK);

            //添加此位置的视图,返回此位置的对象
            container.addView(tv);

            return tv;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return (view == object);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            View page = (View) object;
            container.removeView(page);
        }

    }
}
